import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: () => import("@/views/Login/index") },
  { path: "/home", component: () => import("@/views/Game/index") },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const nickname = localStorage.getItem("nickname");

  if (nickname) {
    next();
  } else {
    if (to.path !== "/login") {
      next({ path: "/login" });
    } else {
      next();
    }
  }
});

export default router;
